package com.glb.gilibuy.interceptor;

import com.glb.gilibuy.web.vo.UserVO;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Description: 登录过滤器
 * @Author: Yingtian qiana
 * @Date: 2021/8/6 13:24
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object handler) throws Exception {
        HttpSession httpSession = request.getSession();
        UserVO user = (UserVO) httpSession.getAttribute("user");
        System.out.println("拦截器拦截的session：" + httpSession);
        if (user != null) {
            System.out.println("拦截器拦截的用户：" + user.toString() + "  " + user.getUserId());
        }
        if (user != null) {
            return true;
        } else {
            response.sendRedirect("/loginPage");
            return false;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request,
                           HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request,
                                HttpServletResponse response, Object handler,
                                Exception ex) throws Exception {

    }
}